package top.dark.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.Query;
import top.dark.product.dao.AttrGroupDao;
import top.dark.product.entity.AttrAttrgroupRelationEntity;
import top.dark.product.entity.AttrEntity;
import top.dark.product.entity.AttrGroupEntity;
import top.dark.product.service.AttrAttrgroupRelationService;
import top.dark.product.service.AttrGroupService;
import top.dark.product.service.AttrService;
import top.dark.product.vo.AttrGroupRespVo;
import top.dark.product.vo.SpuItemAttrGroupVo;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        // 如果分类id为0时表示查询三级分类下所有属性分类信息
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and((item)->{
                item.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (categoryId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id",categoryId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupRespVo> getGroupAndAttrByCatelogId(Long catelogId) {
        // 1、查出分类下所有关联的分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 2、查出分组下关联的所有属性
        List<AttrGroupRespVo> vos = attrGroupEntities.stream().map(item -> {
            AttrGroupRespVo attrGroupRespVo = new AttrGroupRespVo();
            BeanUtils.copyProperties(item, attrGroupRespVo);
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.
                    list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));
            List<Long> attrIds = relationEntities.stream().map(relationEntity -> relationEntity.getAttrId()).collect(Collectors.toList());
            List<AttrEntity> attrEntities = attrService.listByIds(attrIds);
            attrGroupRespVo.setAttrs(attrEntities);
            return attrGroupRespVo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getGroupAndAttrBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getGroupAndAttrBySpuId(spuId, catalogId);
    }

}